package terra.managers;

import org.jboss.netty.util.internal.ConcurrentHashMap;

import terra.Config;
import terra.definitions.GroundDef;
import terra.model.MapObject;


public class MapManager {
	
	private static MapManager instance;
	private ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,MapObject>> map = new ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,MapObject>>();

	public MapManager(){
		clear();
	}
	
	public static MapManager getInstance() {
		if (instance == null)
			return instance = new MapManager();
		return instance;
	}

	public MapObject getTile(int x,int y) {
		if (!getMap().containsKey(y))
			return null;
		return getMap().get(y).get(x);
	}
	
	public void setTile(MapObject tile) {
		if (!getMap().containsKey(tile.y))
			getMap().put(tile.y, new ConcurrentHashMap<Integer,MapObject>());
		getMap().get(tile.y).put(tile.x, tile);
	}
	
	public ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,MapObject>> getMap() {
		return map;
	}

	public GroundDef getDef(int id) {
		return DefManager.groundDefs.get(id);
	}


	public void clear() {
		map.clear();
		for(int i = 0; i < Config.mapHeight;i++)
			map.put(i, new ConcurrentHashMap<Integer,MapObject>());
	}

}
